package com.zt.douyamusic.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.kongzue.dialog.v2.MessageDialog;
import com.zt.douyamusic.R;
import com.zt.douyamusic.bmob.UserBean;
import com.zt.douyamusic.utils.CallPhoneUtils;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class RegisterActivity extends InfoBaseActivity {
    EditText nicknameEdit,mobileEdit,passEdit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        nicknameEdit = (EditText) findViewById(R.id.nickname);
        mobileEdit = (EditText) findViewById(R.id.mobile);
        passEdit = (EditText) findViewById(R.id.pass);
        findViewById(R.id.back_iv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        findViewById(R.id.register_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String nickname = nicknameEdit.getText().toString().trim();
                String mobile = mobileEdit.getText().toString().trim();
                String pass = passEdit.getText().toString().trim();
                if (TextUtils.isEmpty(nickname)){
                    Toast.makeText(RegisterActivity.this, "请输入昵称", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!CallPhoneUtils.isPhone(mobile)){
                    Toast.makeText(RegisterActivity.this, "请填写正确的手机号码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(pass)){
                    Toast.makeText(RegisterActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                UserBean userBean = new UserBean();
                userBean.nickname = nickname;
                userBean.setMobilePhoneNumber(mobile);
                userBean.setUsername(mobile);
                userBean.setPassword(pass);
                userBean.signUp(new SaveListener<UserBean>(){
                    @Override
                    public void done(UserBean user, BmobException e) {
                        if (e == null) {
                            MessageDialog.show(RegisterActivity.this, "提示", "恭喜您！注册成功", "OK，去登录", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    finish();
                                }
                            });
                        } else {
                            MessageDialog.show(RegisterActivity.this, "注册失败", e.getMessage());
                        }
                    }
                });
            }
        });
    }
}
